package com.hup.utils.io.easyexcel;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * @author hugan
 * @date 2019/12/20
 */
@Log4j2
public class EExcelUtil {

    /**
     * 根据名称模糊获取sheet
     *
     * @param sheets  excelReader.excelExecutor().sheetList();
     * @return notNull,如果有多个符合的sheet则只会返回靠前的一个;
     * @throws IllegalArgumentException 找不到表单
     */
    public static ReadSheet getSheetLikeName(List<ReadSheet> sheets, String name) {
        for (ReadSheet sheet : sheets) {
            String sheetName = sheet.getSheetName();
            if (sheetName.contains(name)) {
                return sheet;
            }
        }
        throw new IllegalArgumentException("找不到[" + name + "]表单");
    }

    public static void closeSilent(ExcelReader excelReader) {
        if (excelReader != null) {
            try {
                excelReader.finish();
            } catch (Exception e) {
                log.error("ExcelReader回收失败={}", e.getMessage());
            }
        }
    }

}
